package org.csu.mypetstore.service;


import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.persistence.AccountDAO;
import org.csu.mypetstore.persistence.CartDAO;
import org.csu.mypetstore.persistence.impl.AccountDAOImpl;
import org.csu.mypetstore.persistence.impl.CartDAOImpl;

public class AccountService {

  private AccountDAO accountDAO;
  private CartDAO cartDAO;

  public AccountService(){
    accountDAO = new AccountDAOImpl();
    cartDAO = new CartDAOImpl();
  }

  public Account getAccount(String username) {
    return accountDAO.getAccountByUsername(username);
  }

  public Account getAccount(String username, String password) {
    Account account = new Account();
    account.setUsername(username);
    account.setPassword(password);
    return accountDAO.getAccountByUsernameAndPassword(account);
  }


  public void insertAccount(Account account) {
    accountDAO.insertAccount(account);
    accountDAO.insertProfile(account);
    accountDAO.insertSignon(account);
  }


  public void updateAccount(Account account) {
    accountDAO.updateAccount(account);
    accountDAO.updateProfile(account);

    if (account.getPassword() != null && account.getPassword().length() > 0) {
      accountDAO.updateSignon(account);
    }
  }

  public Cart getCartByAccount(Account account){
    return cartDAO.getCartByUsername(account.getUsername());
  }

  public void insertCartByAccount(Account account,String itemId,int quantity){
    cartDAO.insertCartByUsername(account.getUsername(),itemId,quantity);
  }

  public void updateCartByAccount(Account account,String itemId,int quantity){
    cartDAO.updateCartQuantity(account.getUsername(),itemId,quantity);
  }

  public void deleteCarItemtByAccount(Account account,String itemId){
    cartDAO.deleteCarItemtByUsername(account.getUsername(),itemId);
  }

  public void deleteALL(Account account){
    cartDAO.delteALL(account.getUsername());
  }



}

